<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Advanced initialization</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialAdvancedInitialization.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Advanced initialization<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>This page discusses several advanced methods for initializing matrices. It gives more details on the comma-initializer, which was introduced before. It also explains how to get special matrices such as the identity matrix and the zero matrix.</p>
<h1><a class="anchor" id="TutorialAdvancedInitializationCommaInitializer"></a>
The comma initializer</h1>
<p><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix, vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few or too many coefficients, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> will complain.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Matrix3f</a> m;</div>
<div class="line">m &lt;&lt; 1, 2, 3,</div>
<div class="line">     4, 5, 6,</div>
<div class="line">     7, 8, 9;</div>
<div class="line">std::cout &lt;&lt; m;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga276bae130c142e906ad8f47d24d11c1c"><div class="ttname"><a href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Eigen::Matrix3f</a></div><div class="ttdeci">Matrix&lt; float, 3, 3 &gt; Matrix3f</div><div class="ttdoc">3×3 matrix of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">1 2 3
4 5 6
7 8 9</pre> </td></tr>
</table>
<p>Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is to join vectors or matrices together. For example, here is how to join two row vectors together. Remember that you have to set the size before you can use the comma initializer.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga531c6af4be9297dbe53127a46c246e00">RowVectorXd</a> vec1(3);</div>
<div class="line">vec1 &lt;&lt; 1, 2, 3;</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;vec1 = &quot;</span> &lt;&lt; vec1 &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga531c6af4be9297dbe53127a46c246e00">RowVectorXd</a> vec2(4);</div>
<div class="line">vec2 &lt;&lt; 1, 4, 9, 16;</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;vec2 = &quot;</span> &lt;&lt; vec2 &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga531c6af4be9297dbe53127a46c246e00">RowVectorXd</a> joined(7);</div>
<div class="line">joined &lt;&lt; vec1, vec2;</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;joined = &quot;</span> &lt;&lt; joined &lt;&lt; std::endl;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga531c6af4be9297dbe53127a46c246e00"><div class="ttname"><a href="group__matrixtypedefs.html#ga531c6af4be9297dbe53127a46c246e00">Eigen::RowVectorXd</a></div><div class="ttdeci">Matrix&lt; double, 1, Dynamic &gt; RowVectorXd</div><div class="ttdoc">1×Dynamic vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">vec1 = 1 2 3
vec2 =  1  4  9 16
joined =  1  2  3  1  4  9 16
</pre> </td></tr>
</table>
<p>We can use the same technique to initialize matrices with a block structure.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> matA(2, 2);</div>
<div class="line">matA &lt;&lt; 1, 2, 3, 4;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> matB(4, 4);</div>
<div class="line">matB &lt;&lt; matA, matA/10, matA/10, matA;</div>
<div class="line">std::cout &lt;&lt; matB &lt;&lt; std::endl;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga731599f782380312960376c43450eb48"><div class="ttname"><a href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">Eigen::MatrixXf</a></div><div class="ttdeci">Matrix&lt; float, Dynamic, Dynamic &gt; MatrixXf</div><div class="ttdoc">Dynamic×Dynamic matrix of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">  1   2 0.1 0.2
  3   4 0.3 0.4
0.1 0.2   1   2
0.3 0.4   3   4
</pre> </td></tr>
</table>
<p>The comma initializer can also be used to fill block expressions such as <code>m.row(i)</code>. Here is a more complicated way to get the same result as in the first example above:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Matrix3f</a> m;</div>
<div class="line">m.row(0) &lt;&lt; 1, 2, 3;</div>
<div class="line">m.block(1,0,2,2) &lt;&lt; 4, 5, 7, 8;</div>
<div class="line">m.col(2).tail(2) &lt;&lt; 6, 9;                   </div>
<div class="line">std::cout &lt;&lt; m;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">1 2 3
4 5 6
7 8 9</pre> </td></tr>
</table>
<h1><a class="anchor" id="TutorialAdvancedInitializationSpecialMatrices"></a>
Special matrices and arrays</h1>
<p>The <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> and <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> classes have static methods like <a class="el" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">Zero()</a>, which can be used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional objects. All three variants are illustrated in the following example:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;A fixed-size array:\n&quot;</span>;</div>
<div class="line">Array33f a1 = <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">Array33f::Zero</a>();</div>
<div class="line">std::cout &lt;&lt; a1 &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;A one-dimensional dynamic-size array:\n&quot;</span>;</div>
<div class="line">ArrayXf a2 = <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">ArrayXf::Zero</a>(3);</div>
<div class="line">std::cout &lt;&lt; a2 &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;A two-dimensional dynamic-size array:\n&quot;</span>;</div>
<div class="line">ArrayXXf a3 = <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">ArrayXXf::Zero</a>(3, 4);</div>
<div class="line">std::cout &lt;&lt; a3 &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a422ddeef58bedc7bddb1d4357688d761"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">Eigen::DenseBase::Zero</a></div><div class="ttdeci">static const ConstantReturnType Zero()</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:516</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">A fixed-size array:
0 0 0
0 0 0
0 0 0

A one-dimensional dynamic-size array:
0
0
0

A two-dimensional dynamic-size array:
0 0 0 0
0 0 0 0
0 0 0 0
</pre> </td></tr>
</table>
<p>Similarly, the static method <a class="el" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">Constant</a>(value) sets all coefficients to <code>value</code>. If the size of the object needs to be specified, the additional arguments go before the <code>value</code> argument, as in <code>MatrixXd::Constant(rows, cols, value)</code>. The method <a class="el" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Random() </a> fills the matrix or array with random coefficients. The identity matrix can be obtained by calling <a class="el" href="classEigen_1_1MatrixBase.html#a98bb9a0f705c6dfde85b0bfff31bf88f">Identity()</a>; this method is only available for <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>, not for <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a>, because "identity matrix" is a linear algebra concept. The method <a class="el" href="classEigen_1_1DenseBase.html#a1c6d1dbfeb9f6491173a83eb44e14c1d">LinSpaced</a>(size, low, high) is only available for vectors and one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between <code>low</code> and <code>high</code>. The method <code>LinSpaced()</code> is illustrated in the following example, which prints a table with angles in degrees, the corresponding angle in radians, and their sine and cosine.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">ArrayXXf table(10, 4);</div>
<div class="line">table.col(0) = <a class="code" href="classEigen_1_1DenseBase.html#a1c6d1dbfeb9f6491173a83eb44e14c1d">ArrayXf::LinSpaced</a>(10, 0, 90);</div>
<div class="line">table.col(1) = M_PI / 180 * table.col(0);</div>
<div class="line">table.col(2) = table.col(1).sin();</div>
<div class="line">table.col(3) = table.col(1).cos();</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;  Degrees   Radians      Sine    Cosine\n&quot;</span>;</div>
<div class="line">std::cout &lt;&lt; table &lt;&lt; std::endl;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a1c6d1dbfeb9f6491173a83eb44e14c1d"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a1c6d1dbfeb9f6491173a83eb44e14c1d">Eigen::DenseBase::LinSpaced</a></div><div class="ttdeci">static EIGEN_DEPRECATED const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &amp;low, const Scalar &amp;high)</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:246</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">  Degrees   Radians      Sine    Cosine
        0         0         0         1
       10     0.175     0.174     0.985
       20     0.349     0.342      0.94
       30     0.524       0.5     0.866
       40     0.698     0.643     0.766
       50     0.873     0.766     0.643
       60      1.05     0.866       0.5
       70      1.22      0.94     0.342
       80       1.4     0.985     0.174
       90      1.57         1 -4.37e-08
</pre> </td></tr>
</table>
<p>This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and expressions). <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> defines utility functions like <a class="el" href="classEigen_1_1DenseBase.html#af230a143de50695d2d1fae93db7e4dcb">setZero()</a>, <a class="el" href="classEigen_1_1MatrixBase.html#a18e969adfdf2db4ac44c47fbdc854683">MatrixBase::setIdentity()</a> and <a class="el" href="classEigen_1_1DenseBase.html#a5d1ce9e801fa502e02b9b8cd9141ad0a">DenseBase::setLinSpaced()</a> to do this conveniently. The following example contrasts three ways to construct the matrix \( J = \bigl[ \begin{smallmatrix} O &amp; I \\ I &amp; O \end{smallmatrix} \bigr] \): using static methods and assignment, using static methods and the comma-initializer, or using the setXxx() methods.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> size = 6;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a> mat1(size, size);</div>
<div class="line">mat1.topLeftCorner(size/2, size/2)     = <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">MatrixXd::Zero</a>(size/2, size/2);</div>
<div class="line">mat1.topRightCorner(size/2, size/2)    = <a class="code" href="classEigen_1_1MatrixBase.html#a98bb9a0f705c6dfde85b0bfff31bf88f">MatrixXd::Identity</a>(size/2, size/2);</div>
<div class="line">mat1.bottomLeftCorner(size/2, size/2)  = <a class="code" href="classEigen_1_1MatrixBase.html#a98bb9a0f705c6dfde85b0bfff31bf88f">MatrixXd::Identity</a>(size/2, size/2);</div>
<div class="line">mat1.bottomRightCorner(size/2, size/2) = <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">MatrixXd::Zero</a>(size/2, size/2);</div>
<div class="line">std::cout &lt;&lt; mat1 &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a> mat2(size, size);</div>
<div class="line">mat2.topLeftCorner(size/2, size/2).setZero();</div>
<div class="line">mat2.topRightCorner(size/2, size/2).setIdentity();</div>
<div class="line">mat2.bottomLeftCorner(size/2, size/2).setIdentity();</div>
<div class="line">mat2.bottomRightCorner(size/2, size/2).setZero();</div>
<div class="line">std::cout &lt;&lt; mat2 &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a> mat3(size, size);</div>
<div class="line">mat3 &lt;&lt; <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">MatrixXd::Zero</a>(size/2, size/2), <a class="code" href="classEigen_1_1MatrixBase.html#a98bb9a0f705c6dfde85b0bfff31bf88f">MatrixXd::Identity</a>(size/2, size/2),</div>
<div class="line">        <a class="code" href="classEigen_1_1MatrixBase.html#a98bb9a0f705c6dfde85b0bfff31bf88f">MatrixXd::Identity</a>(size/2, size/2), <a class="code" href="classEigen_1_1DenseBase.html#a422ddeef58bedc7bddb1d4357688d761">MatrixXd::Zero</a>(size/2, size/2);</div>
<div class="line">std::cout &lt;&lt; mat3 &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="ttc" id="aclassEigen_1_1MatrixBase_html_a98bb9a0f705c6dfde85b0bfff31bf88f"><div class="ttname"><a href="classEigen_1_1MatrixBase.html#a98bb9a0f705c6dfde85b0bfff31bf88f">Eigen::MatrixBase::Identity</a></div><div class="ttdeci">static const IdentityReturnType Identity()</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:801</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga99b41a69f0bf64eadb63a97f357ab412"><div class="ttname"><a href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">Eigen::MatrixXd</a></div><div class="ttdeci">Matrix&lt; double, Dynamic, Dynamic &gt; MatrixXd</div><div class="ttdoc">Dynamic×Dynamic matrix of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0

0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0

0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
</pre> </td></tr>
</table>
<p>A summary of all pre-defined matrix, vector and array objects can be found in the <a class="el" href="group__QuickRefPage.html">Quick reference guide</a>.</p>
<h1><a class="anchor" id="TutorialAdvancedInitializationTemporaryObjects"></a>
Usage as temporary objects</h1>
<p>As shown above, static methods as Zero() and Constant() can be used to initialize variables at the time of declaration or at the right-hand side of an assignment operator. You can think of these methods as returning a matrix or array; in fact, they return so-called <a class="el" href="TopicEigenExpressionTemplates.html">expression objects</a> which evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.</p>
<p>These expressions can also be used as a temporary object. The second example in the <a class="el" href="GettingStarted.html">Getting started</a> guide, which we reproduce here, already illustrates this.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">Eigen::MatrixXd</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">Eigen::VectorXd</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a> m = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXd::Random</a>(3,3);</div>
<div class="line">  m = (m + <a class="code" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">MatrixXd::Constant</a>(3,3,1.2)) * 50;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;m =&quot;</span> &lt;&lt; std::endl &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">VectorXd</a> v(3);</div>
<div class="line">  v &lt;&lt; 1, 2, 3;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;m * v =&quot;</span> &lt;&lt; std::endl &lt;&lt; m * v &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a68a7ece6c5629d1e9447a321fcb14ccd"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">Eigen::DenseBase::Constant</a></div><div class="ttdeci">static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &amp;value)</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:191</div></div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_ae814abb451b48ed872819192dc188c19"><div class="ttname"><a href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::DenseBase::Random</a></div><div class="ttdeci">static const RandomReturnType Random()</div><div class="ttdef"><b>Definition:</b> Random.h:114</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga8554c6170729f01c7572574837ecf618"><div class="ttname"><a href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">Eigen::VectorXd</a></div><div class="ttdeci">Matrix&lt; double, Dynamic, 1 &gt; VectorXd</div><div class="ttdoc">Dynamic×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">m =
  94 89.8 43.5
49.4  101 86.8
88.3 29.8 37.8
m * v =
404
512
261
</pre> </td></tr>
</table>
<p>The expression <code>m + <a class="el" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">MatrixXf::Constant</a>(3,3,1.2)</code> constructs the 3-by-3 matrix expression with all its coefficients equal to 1.2 plus the corresponding coefficient of <em>m</em>.</p>
<p>The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random matrix of size 2-by-3, and then multiplies this matrix on the left with \( \bigl[ \begin{smallmatrix} 0 &amp; 1 \\ 1 &amp; 0 \end{smallmatrix} \bigr] \).</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> mat = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXf::Random</a>(2, 3);</div>
<div class="line">std::cout &lt;&lt; mat &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line">mat = (<a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a>(2,2) &lt;&lt; 0, 1, 1, 0).finished() * mat;</div>
<div class="line">std::cout &lt;&lt; mat &lt;&lt; std::endl;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">  0.68  0.566  0.823
-0.211  0.597 -0.605

-0.211  0.597 -0.605
  0.68  0.566  0.823
</pre> </td></tr>
</table>
<p>The <a class="el" href="structEigen_1_1CommaInitializer.html#a3cf9e2b8a227940f50103130b2d2859a">finished() </a> method is necessary here to get the actual matrix object once the comma initialization of our temporary submatrix is done. </p>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
